<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers;
use App\Models\Admin;
use App\Models\Adminmenu;
use App\Models\Announce;
use App\Models\siteconfig;
use Illuminate\Support\Facades\DB;

Class SiteConfigController extends BaseController{

      public function __construct(Adminmenu $adminmenu , siteconfig $siteconfig , Announce $announce , Admin $admin)
      {
          $this->adminmenu = $adminmenu;
          $this->siteconfig = $siteconfig;
          $this->announce = $announce;
          $this->admin = $admin;
      }


    /**
     * 返回首页视图
     */
      public function getIndex(){
           return view('admin.index.index',compact('adminqmenus'));
      }

    //设置
    public function getAdminSyspara()
    {
        $menuchild =  $this->adminmenu->getChildMenuByParentid(request('meneid'));
        $siteConfig = $this->siteconfig->getConfig();
        return view('admin.siteconfig.config_index',compact('menuchild','siteConfig'));
    }

    public function save_syspara()
    {
        $siteconfig = D('Siteconfig');
        $vo = $siteconfig->create();
        if(!$vo) {
            $this->assign('jumpUrl',__URL__.'/admin_syspara/');
            $this->error('修改失败');
        }else{
            $dn = $siteconfig->where("id=1")->save($vo);
            $cdn=$_POST['cdn'];
            $fps=$_POST['fps'];
            $zddk=$_POST['zddk'];
            $pz=$_POST['pz'];
            $zjg=$_POST['zjg'];
            $cdnl=$_POST['cdnl'];
            $height=$_POST['height'];
            $width=$_POST['width'];
            $sql="update ss_siteconfig set cdn='{$cdn}',fps='{$fps}',zddk='{$zddk}',pz='{$pz}',zjg='{$zjg}',cdnl='{$cdnl}',height='{$height}',width='{$width}' where id=1";
            M('siteconfig')->execute($sql);
            $this->assign('jumpUrl',__URL__.'/admin_syspara/');
            $this->success('修改成功');
        }
    }

    /**
     * 服务器缓存设置
     */
    public function getAdminRtmpserver(){
        $servers = DB::table('server')->find(1);
        $menuchild =  $this->adminmenu->getChildMenuByParentid(request('meneid'));
        return view('admin.siteconfig.admin_rtmpserver',compact('menuchild','servers'));
    }

    /**
     * 服务器缓存保存
     */
    public function getAdminRtmpserverSave(){
        $menuchild =  $this->adminmenu->getChildMenuByParentid(request('meneid'));
        return view('admin.siteconfig.admin_cacheset',compact('menuchild'));
    }

    /**
     * 直播服务器设置
     */
    public function getAdminCacheset(){
        $config = $this->siteconfig->getConfig();
        $menuchild =  $this->adminmenu->getChildMenuByParentid(request('meneid'));
        return view('admin.siteconfig.admin_cacheset',compact('menuchild','config'));
    }

    /**
     * 直播服务器保存
     */
    public function getAdminCachesetSave(){
        $menuchild =  $this->adminmenu->getChildMenuByParentid(request('meneid'));
        return view('admin.siteconfig.admin_cacheset',compact('menuchild'));
    }

    /**
     * 收益成分设置
     */
    public function getAdminDeduct(){
        $config = $this->siteconfig->getConfig();
        $menuchild =  $this->adminmenu->getChildMenuByParentid(request('meneid'));
        return view('admin.siteconfig.admin_deduct',compact('menuchild','config'));
    }

    /**
     * 收益成分保存
     */
    public function getAdminDeductSave(){


    }

    /**
     * 轮播图设置
     */
    public function getAdminRollpic(){
        $menuchild =  $this->adminmenu->getChildMenuByParentid(request('meneid'));
        $rollkist = DB::table('rollpic')->get();
        return view('admin.siteconfig.admin_rollpic',compact('menuchild','rollkist'));
    }

    /**
     * 轮播图设置保存
     */
    public function getAdminRollpicSave(){
        $menuchild =  $this->adminmenu->getChildMenuByParentid(request('meneid'));

        return view('admin.siteconfig.admin_cacheset',compact('menuchild'));
    }

    /**
     *  轮播图上传
     */
    public function postSaveRollpicSample(){
        $imagesdata = $_POST['imagesdata'];
        $id = $_POST['upload_id'];
        $imagesLink = $_POST['imageslink'];
        $imagesarr[0] = $imagesdata;
        $prefix = date('Y-m-d',time()); // 每天生成一个文件夹
        $dir = '/upload/rollpic/'.$prefix.'/';
        $upload_info = $this->upImgs($imagesarr,$dir);
        $rollpic = DB::table('rollpic');
        if ($id) {
            $rollpic->find($id);
            $rollpic->picpath = $upload_info[0];
            $rollpic->linkurl = $imagesLink;
            $rollpic->save();
            unlink($rollpic['picpath']);
        }else{
            $rollpic->picpath = $upload_info[0];
            $rollpic->title = "";
            $rollpic->description = "";
            $rollpic->linkurl = $imagesLink;
            $rollpic->orderno = 0;
            $rollpic->addtime = time();
            $res = $rollpic->create();
            if ($res) {
                return json_encode(['code'=>200,'msg'=>'上传成功']);
            }else{
                return json_encode(['code'=>400,'msg'=>'上传失败']);
            }
        }
    }

    /**
     * 收益成分保存
     */
    public function getAdminAnnounce(){
        $announce = $this->announce->getAnnounceList(15);
        $announce = $announce->toArray();
        $menuchild =  $this->adminmenu->getChildMenuByParentid(request('meneid'));
        return view('admin.siteconfig.admin_announce',compact('menuchild','announce'));
    }

    public function getAddAnnounce(){
        $menuchild =  $this->adminmenu->getChildMenuByParentid(request('meneid'));
        $huodong = DB::table('huodongfenlei')->get();
        return view('admin.siteconfig.addannounce',compact('menuchild','huodong'));
    }


    public function getEditAnnounce(){
        $announinfo = DB::table('announce')->find(request('annid'));
        return view('admin.siteconfig.edit_announce',compact('announinfo'));
    }

    public function postEdit(){
        $res = $this->announce->eidtannounce(request());
        if ($res['code']==200) {
            echo "修改成功";
            die();
        }
        echo "修改失败";
    }


    public function postInsert(){
        $this->announce->eidtannounce(request());
    }

    public function getAdminAdmin(){
        $adminlist = $this->admin->getadminlist();
        return view('admin.siteconfig.adminlist',compact('adminlist'));
    }


    public function getAddadminuser(){
           return view('admin.siteconfig.addadminuser');
    }

    public function getEditadminuser(){
        $admininfo = DB::table('admin')->find(request('adminid'));
        return view('admin.siteconfig.editadminuser',compact('admininfo'));
    }

    public function postEditadminuser(){
        dd($_POST);
    }

    public function postAddadminuser(){
         $res = $this->admin->addadminuser(request());
         if ($res['code']==400){
              echo '添加失败';
         }else{
             return redirect('admin/siteconfig/admin-admin');
         }
    }
}